from django import template

register = template.Library()

@register.filter(name='add_class')
def add_class(field, class_name):
    attrs = field.field.widget.attrs
    if 'class' in attrs:
        attrs['class'] += ' ' + class_name
    else:
        attrs['class'] = class_name
    field.field.widget.attrs = attrs
    return field